package com.sixmac.controller;

import com.sixmac.annotation.AuthUser;
import com.sixmac.common.base.BaseController;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.plugins.dao.Db;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.grid.BladePage;
import com.sixmac.module.Landlord;
import com.sixmac.module.RoomRepair;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created by admin on 2018/9/4.
 */
@Controller
@ResponseBody
@RequestMapping("/repair")
public class RepairController extends BaseController {


    /**
     * @api {post} /repair/index 0.1 报修管理
     * @apiName repair.index
     * @apiGroup repair
     * @apiParam {String} token token
     * @apiParam {String} page page
     * @apiParam {String} size size
     * @apiSuccess {String} user.code 111表示用户第一次登陆，需要创建用户 调用接口03
     * @apiSuccess {String} user.id 报修id
     * @apiSuccess {String} user.name 房客名称
     * @apiSuccess {String} user.address 地址
     * @apiSuccess {String} user.status 1待受理2已完成
     * @apiSuccess {String} user.images 房屋图片，多个用,分隔
     * @apiSuccess {String} user.content 内容
     * @apiSuccess {String} user.mobile 手机号码
     * @apiSuccess {String} user.createDate 日期
     */
    @AuthUser
    @RequestMapping("/index")
    public AjaxResult index(Landlord landlord, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size) {
        BladePage<Map> bladePage = Db.paginate(" select r.*,t.name as name,t.mobile,room.address from t_room_repair r left join t_tenant t on t.id = r.tenantId left join t_room room on room.id = r.roomId where r.landlordId=" + landlord.getId(), null, page, size);
        return json(bladePage);
    }

    /**
     * @api {post} /repair/passRepair 0.2 报修完成
     * @apiName repair.passRepair
     * @apiGroup repair
     * @apiParam {String} token token
     * @apiParam {String} repairId 报修id
     */
    @AuthUser
    @RequestMapping("/passRepair")
    public AjaxResult passRepair(Landlord landlord, Integer repairId) {
        RoomRepair roomRepair = Blade.create(RoomRepair.class).findById(repairId);
        //是否完成
        if (roomRepair.getStatus() == 2)
            return error("该问题已处理");
        roomRepair.setStatus(2);
        //改报修是否属于该房东
        if (roomRepair.getLandlordId().compareTo(landlord.getId()) != 0)
            return error("该房间不属于您，请联系系统服务人员。");
        Blade.create(RoomRepair.class).update(roomRepair);
        return success("维修确定成功");
    }


}
